<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta name="Description" content="Java Concurrency, Java Testing" />
<meta name="Keywords" content="junit, concurrent, java, concurrent abstractions, testing framework, ConTest, GroboUtils, ActiveTest, JavaOne" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Distribution" content="Global" />
<meta name="Author" content="Erwin Aligam - ealigam@gmail.com" />
<meta name="Author" content="Nat Ayewah" />
<meta name="Robots" content="index,follow" />

<link rel="stylesheet" href="images/Envision.css" type="text/css" />

<title>Overview of MultithreadedTC - Dept. of Computer Science, UMD</title>

</head>

<body>

<!-- wrap starts here -->
<div id="wrap">
<a name="pagetop"></a>

		<!--header -->
		<div id="header">
		
			<img id="logobox" src="images/mtclogo.png" />
			<h1 id="logo-text">MultithreadedTC</h1>
			<h2 id="slogan">A framework for testing concurrent Java applications</h2>			
			
			<div id="header-links">
			<p>
				<a href="index.html">Home</a> |
				<a href="http://www.cs.umd.edu/projects/PL/">PL Group</a> |
				<a href="http://www.cs.umd.edu/">UMD CS</a>
			</p>
			</div>

		</div>

		<!-- menu -->
		<div  id="menu">
			<ul>
				<li><a href="index.html">Home</a></li>
				<li id="current"><a href="overview.html">Overview</a></li>
				<li><a href="http://code.google.com/p/multithreadedtc/downloads/list">Downloads</a></li>
				<li class="last"><a href="http://code.google.com/p/multithreadedtc/source">SVN Repository</a></li>
			</ul>
		</div>

		<!-- content-wrap starts here -->
		<div id="content-wrap">

			<div id="sidebar">

				<h1>Overview</h1>
				<ul class="sidemenu">
					<li>1. <a href="#introduction1">What is MultithreadedTC?</a></li>
					<li>2. <a href="#howitworks2">How does it work?</a></li>
					<li>3. <a href="#usage3">How do I use it?</a></li>
					<li>4. <a href="#examples4">Examples</a></li>
				</ul>


				<h1>MultithreadedTC</h1>
				<ul class="sidemenu">
					<li><a href="index.html">Home</a></li>
					<li><a href="overview.html">Overview</a></li>
					<li><a href="http://code.google.com/p/multithreadedtc/downloads/list">Downloads</a></li>
					<li><a href="http://code.google.com/p/multithreadedtc/source">SVN Repository</a></li>
				</ul>


				<h1>Docs and Info</h1>
				<ul class="sidemenu">
					<li><a href="docs/index.html">Javadoc Documentation</a></li>
					<!-- <li><a href="http://#">JavaOne Presentation</a></li> -->
					<!-- <li><a href="http://#">ASE Paper</a></li> -->
				</ul>
				<p>&nbsp;</p>

			</div>

			<div id="main">

				<a name="introduction1"></a>
				<h1>1. What is MultithreadedTC?</h1>
				
				<blockquote><p><strong>MultithreadedTC</strong> is a framework for testing concurrent applications. It
				features a metronome that is used to provide fine control over the sequence of
				activities in multiple threads.</p></blockquote>
				
				<p class="quicklink">(OK I think I get it. <a href="#examples4">Take me straight to some examples.</a>)</p>
				
				<p>Many failures in concurrent applications are not deterministic. They do not
				occur every time the application is run. Different interleavings of application threads yield different
				behaviors.</p>
				<p>Concurrent application designers often want to run many (unrelated or loosely
				related) threads of activity to maximize throughput. Sometimes it is useful for
				test designers to mimic this style and run multiple threads, generating as many
				interleavings as possible. Many test frameworks support this paradigm (e.g.
				<a href="http://www.alphaworks.ibm.com/tech/contest">IBM's ConTest</a>, 
				<a href="http://groboutils.sourceforge.net/testing-junit/using_mtt.html">GroboUtils' MultiThreadedTestRunner</a>, 
				and
				<a href="http://junit.sourceforge.net/javadoc/junit/extensions/ActiveTestSuite.html">
				JUnit's ActiveTestSuite</a>).</p>
				<p><strong>MultithreadedTC</strong> is different. It supports test cases that exercise a
				<i>specific</i> interleaving of threads. This is motivated by the principle that
				concurrent applications should be built using <i>small concurrent abstractions</i>
				such as bounded buffers, semaphores and latches. Separating the concurrency
				logic from the rest of the application logic in this way makes it easier to
				understand and test concurrent applications. Since these abstractions are small,
				it should be possible to deterministically test every (significant) interleaving
				in separate tests.</p>
				<p>But how can one guarantee a specific interleaving of different threads in the
				presence of blocking and timing issues? Consider the following example of some operations
				on a bounded blocking buffer (e.g. <code>ArrayBlockingQueue</code>) with capacity 1:</p>
				
				<table>
					<tr class="row-a">
						<td>
						<img border="0" src="images/boundedbuffer1.png"></td>
					</tr>
				</table>
				
				<p>We want a test to confirm that <code>put 17</code> causes Thread 1 to block until
				<code>take 42</code> occurs in Thread 2. The
				test must guarantee that <code>take 42</code> is not executed until
				<i>after</i> <code>put 17</code>. How could a designer guarantee this interleaving of
				the two threads? </p>
				<p>One approach is to use <code>Thread.sleep()</code> in Thread 2 to delay its first
				statement long enough to &quot;guarantee&quot; that Thread 1 has blocked. But this
				approach makes the test timing-dependent &#8213; timing can be thrown off by, say, an
				ill-timed garbage collector. This also does not work well when stepping through
				the code in a debugger. </p>
				<p>Another common approach for coordinating activities in two threads is to use
				a <code>CountDownLatch.</code> A <code>CountDownLatch</code> will not work in this example as illustrated
				by the following pseudocode:</p>								
				
				<table>
					<tr class="row-b">
						<td colspan="2">Initialization</td>
					</tr>
					<tr class="row-a">
						<td colspan="2">
							<code class="codeBlock">
								ArrayBlockingQueue buf = new ArrayBlockingQueue(1); <br />
								CountDownLatch c = new CountDownLatch(1);
							</code>
						</td>
					</tr>
					<tr class="row-b">
						<td>Thread 1</td> <td>Thread 2</td>
					</tr>
					<tr class="row-a">
						<td>
							<code class="codeBlock">
								buf.put(42); <br />
								buf.put(17); <br />
								c.countDown();<br /><br /><br />
							</code>
						</td>
						<td>
							<code class="codeBlock">
								c.await(); <br /><br /><br />
								assertEquals(42, buf.take()); <br />
								assertEquals(17, buf.take());
							</code>
						</td>
					</tr>
				</table>
									
									
				<p>Of course the problem is that the statement <code>c.countDown()</code> cannot be executed
				until after Thread 1 unblocks... which will not occur until Thread 2 <code>take()</code>s. In
				other words, this test is deadlocked! </p>
				<p><strong>MultithreadedTC</strong> provides an elegant solution to this problem, illustrated in
				the following example:</p>

				<table>
					<tr class="row-a">
						<td>
							<div class="java"><code class="java"><span class="java4">class </span><span class="java10">MTCBoundedBufferTest </span><span class="java4">extends </span><span class="java10">MultithreadedTestCase </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">ArrayBlockingQueue&lt;Integer&gt; buf;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">initialize</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">buf = </span><span class="java4">new </span><span class="java10">ArrayBlockingQueue&lt;Integer&gt;</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">; <br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread1</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">InterruptedException </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">buf.put</span><span class="java8">(</span><span class="java7">42</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; buf.put</span><span class="java8">(</span><span class="java7">17</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread2</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">InterruptedException </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">Integer.valueOf</span><span class="java8">(</span><span class="java7">42</span><span class="java8">)</span><span class="java10">, buf.take</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">Integer.valueOf</span><span class="java8">(</span><span class="java7">17</span><span class="java8">)</span><span class="java10">, buf.take</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">finish</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertTrue</span><span class="java8">(</span><span class="java10">buf.isEmpty</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}</span></code></div>
						</td>
					</tr>
					<tr class="row-b">
						<td>This example is illustrated by the following diagram:</td>
					</tr>
					<tr class="row-a">
						<td>
						<img border="0" src="images/boundedbuffer2.png"></td>
					</tr>
				</table>
								
				<p>Multithreaded has an internal metronome (or clock). But don't try and use it
				to set the tempo for your jazz band. <b>The clock only advances to the next tick
				when all threads are blocked</b>. </p>
				<p>The clock starts at tick 0. In this example, the statement <code>waitForTick(1)</code>
				makes Thread 2 block until the clock reaches tick 1 before resuming.
				Thread 1 is allowed to run freely in tick 0, until it blocks on the call to <code>put(17)</code>.
				At this point, all threads are blocked, and the clock can advance to the next
				tick. </p>
				<p>In tick 1, the statement <code>take(42)</code> in Thread 2 is executed, and this frees up Thread
				1. The final statement in Thread 1 asserts that the clock is in tick 1, in
				effect asserting that the thread blocked on the call to <code>put(17)</code>. </p>
				<p>This approach does not deadlock like the <code>CountDownLatch</code>, and is more reliable
				than <code>Thread.sleep()</code>. Some other high level observations are:</p>
				<ul>
					<li>The test is encapsulated in a class that extends <code>MultithreadedTestCase</code>.
					Each of the threads is represented by a method whose name starts with
					&quot;<code>thread</code>&quot;, returns <code>void,</code> and has no arguments. The
					<code>initialize()</code> method is
					invoked first; then the thread methods are invoked simultaneously in
					different threads; finally the <code>finish()</code> method is invoked when all threads
					have completed. </li>
					<li>This test can be run using the following JUnit test:
				
				<div class="java"><code class="codeBlock"><span class="java0">&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">testMTCBoundedBuffer</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Throwable </span><span class="java8">{ <br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">TestFramework.runOnce</span><span class="java8">( </span><span class="java4">new </span><span class="java10">MTCBoundedBufferTest</span><span class="java8">() )</span><span class="java10">; <br />
				&#xA0;&#xA0;&#xA0; </span><span class="java8">}</span></code></div>
				
					This creates an instance of the test class and passes it to the <code>TestFramework</code>.
					The <code>TestFramework</code> creates the necessary threads, manages the metronome, and runs
					the test.
					</li>
					<li>All the components of the test are represented using classes and methods,
				constructs that are recognizable to Java programmers. </li>
				<li>The framework handles exceptions thrown by any of the threads, and
				propagates them up to JUnit. This solves a problem with anonymous Threads, whose
				exceptions are not detected by JUnit without some extra scaffolding provided by
				the test designer. (See <a href="#examples2to4">Examples 2 to 4</a>). </li>
					<li>The clock is not necessarily incremented by units of one. When all threads
				are blocked it advances to the next requested tick specified by a <code>waitForTick()</code>
				method. If none of the threads are waiting for a tick, the test is declared to
				be in deadlock (unless one of the threads is in state <code>TIMED_WAITING</code>).</li>
				</ul>
								
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				
				<a name="howitworks2"></a>
				<h1>2. So how does this work?</h1>
				<p>The class <code>TestFramework</code>, provides most of the scaffolding required to run
				<strong>MultithreadedTC</strong> tests. It uses reflection to identify all relevant methods in
				the test class, invokes them simultaneously in different threads. It regulates
				these threads using a separate <i>clock thread</i>. </p>
				<p>The clock thread checks periodically to see if all threads are blocked. If
				all threads are blocked and at least one is waiting for a tick, the clock thread
				advances the clock to the next desired tick. The clock thread also detects
				deadlock (when all threads are blocked, none are waiting for a tick, and none
				are in state <code>TIMED_WAITING</code>), and can stop a test that is going on too long (a
				thread is in state <code>RUNNABLE</code> for too long.) </p>
				<p>The test threads are placed into a new thread group, and any threads created
				by these test cases will be placed in this thread group by default. All threads
				in the thread group will be considered by the clock thread when deciding whether
				to advance the clock, declare a deadlock, or stop a long-running test.</p>
				
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				<a name="usage3"></a>
				<h1>3. Cool! How do I use this?</h1>
				<p><strong>MultithreadedTC</strong> tests are created by extending one of two classes: </p>
				<ol>
					<li>class <code>MultithreadedTestCase</code> extends <code>junit.framework.Assert</code> and provides
					the base functionality required by all tests. (NOTE: <code>MultithreadedTestCase</code> does
					NOT extend <code>junit.framework.TestCase</code>). A test using this class consists of:
					<ul>
						<li>an optional <code>initialize()</code> method, </li>
						<li>one or more &quot;thread&quot; methods which are invoked in different threads,
						</li>
						<li>an optional <code>finish()</code> method that is run after all threads have
						completed.</li>
					</ul>In addition to the <code>MultithreadedTestCase</code> subclass, an additional JUnit
					test method is used to call one of the &quot;run&quot; methods in <code>TestFramework</code>. The
					run methods receive an instance of a <code>MultithreadedTestCase</code> and test it in
					different ways. The primary run methods are:
					<ul>
						<li><code>runOnce(MultithreadedTestCase)</code> -- run a test sequence once, </li>
						<li><code>runManyTimes(MultithreadedTestCase, int)</code> -- run a test sequence as
						many times as specified by the <code>int</code> parameter, until one of the test runs
						fails. <br>
				&nbsp;</li>
					</ul></li>
					<li>class <code>MultithreadedTest</code> extends <code>MultithreadedTestCase</code> and implements
					<code>junit.framework.Test</code>. So it can be added to a <code>junit.framework.TestSuite</code> and
					run directly. <br>
					<br>
					This class includes a <code>runTest()</code> method that calls:
					<code>TestFramework.runOnce(this)</code>. To change the way a test is run, override the
					<code>runTest()</code> method. <br>
					<br>
										(<strong>MultithreadedTC</strong> provides a convencience
					method: <code>TestFramework.buildTestSuite(...)</code>, that looks for
					inner-classes that implement <code>junit.framework.Test</code> and builds a
					<code>TestSuite</code> out of these classes.)</li>
				</ol>
				
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				<a name="examples4"></a>
				<h1>4. Some Examples of Test Cases</h1>
				
				<h2>Example 1: Compare And Set</h2>
				<p>Here is a simple example to demonstrate the basic layout of a <strong>MultithreadedTC</strong> 
				test. In this example, one thread stays in a while loop until its condition is 
				met due to an action in another thread. Both threads are started simultaneously.</p>
				<p>Notice that in the <strong>MultithreadedTC</strong> version, the <code>AtomicInteger</code> parameter is 
				initialized in the <code>initialize()</code> method, not in a constructor. This is so that if 
				the test is run many times (with <code>TestFramework.runManyTimes(...)</code>), a fresh 
				instance of <code>AtomicInteger</code> is used each time.</p>
				<p>This simple example demonstrates that <strong>MultithreadedTC</strong> takes care of much of 
				the scaffolding work needed to set up and start a thread, and eliminates the 
				need to join the thread before the test ends.</p>
				
				<table>
					<tr class="row-b">
						<td>
							MultithreadedTC Version
						</td>
					</tr>
					<tr class="row-a">
						<td>
				
				<div class="java"><code class="java"><span class="java4">class </span><span class="java10">MTCCompareAndSet </span><span class="java4">extends </span><span class="java10">MultithreadedTest </span><span class="java8">{<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java10">AtomicInteger ai;<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">initialize</span><span class="java8">() {<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">ai = </span><span class="java4">new </span><span class="java10">AtomicInteger</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
				<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread1</span><span class="java8">() {<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">while</span><span class="java8">(</span><span class="java10">!ai.compareAndSet</span><span class="java8">(</span><span class="java7">2</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)) </span><span class="java10">Thread.yield</span><span class="java8">()</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
				<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread2</span><span class="java8">() {&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">1</span><span class="java10">, </span><span class="java7">2</span><span class="java8">))</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
				<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">finish</span><span class="java8">() {<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <br />
				&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
				}</span></code></div>
				
						</td>
					</tr>				
					<tr class="row-b">
						<td>
							Plain Version
						</td>
					</tr>
					<tr class="row-a">
						<td>
				
				<div class="java"><code class="java"><span class="java4">public </span><span class="java9">void </span><span class="java10">testCompareAndSet</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">InterruptedException </span><span class="java8">{<br />
				&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">AtomicInteger ai = </span><span class="java4">new </span><span class="java10">AtomicInteger</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; Thread t = </span><span class="java4">new </span><span class="java10">Thread</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Runnable</span><span class="java8">() {<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">run</span><span class="java8">() {<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">while</span><span class="java8">(</span><span class="java10">!ai.compareAndSet</span><span class="java8">(</span><span class="java7">2</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)) <br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">Thread.yield</span><span class="java8">()</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
				&#xA0;&#xA0;&#xA0; })</span><span class="java10">;<br />
				<br />
				&#xA0;&#xA0;&#xA0; t.start</span><span class="java8">()</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">1</span><span class="java10">, </span><span class="java7">2</span><span class="java8">))</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; t.join</span><span class="java8">(</span><span class="java7">2500</span><span class="java8">)</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; assertFalse</span><span class="java8">(</span><span class="java10">t.isAlive</span><span class="java8">())</span><span class="java10">;<br />
				&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;<br />
				</span><span class="java8">}</span></code></div>
				
						</td>
					</tr>
				</table>

				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				<a name="examples2to4"></a>
				<h2>Example 2: Interrupted Acquire</h2>
				<p>This example shows some of the strengths of <strong>MultithreadedTC</strong>. An <code>acquire()</code> on 
				a <code>Semaphore</code> in one thread is expected to block, and throw an
				<code>InterruptedException</code> when interrupted by another thread.</p>
				<p>The <strong>MultithreadedTC</strong> version can assert that the 
				<code>InterruptedException</code> is not 
				thrown until tick 1, which is when Thread 1 is interrupted. (So an 
				implementation of acquire that unconditionally throws the exception may pass the 
				plain version but will not pass the <strong>MultithreadedTC</strong> version.)
				<code>waitForTick()</code> is used instead of <code>Thread.sleep()</code> 
				to eliminate timing dependencies.</p>
				<p>Notice also that an unchecked <code>AssertionError</code> (caused by the 
				<code>fail(...)</code> 
				statement in Thread 1) will cause the test to kill all threads and fail 
				immediately. In the plain version, this failure kills the auxillary thread but 
				not the test. Some extra work is needed to set a flag indicating thread failure, 
				and assert the flag in JUnit's <code>tearDown()</code> method.</p>

				<table>
					<tr class="row-b">
						<td>
							MultithreadedTC Version
						</td>
					</tr>
					<tr class="row-a">
						<td>
						
							<div class="java"><code class="java"><span class="java4">class </span><span class="java10">MTCInterruptedAcquire </span><span class="java4">extends </span><span class="java10">MultithreadedTestCase </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">Semaphore s;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">initialize</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">s = </span><span class="java4">new </span><span class="java10">Semaphore</span><span class="java8">(</span><span class="java7">0</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread1</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">s.acquire</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; fail</span><span class="java8">(</span><span class="java5">&#34;should throw exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch</span><span class="java8">(</span><span class="java10">InterruptedException success</span><span class="java8">){ </span><span class="java10">assertTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">; </span><span class="java8">}<br />
							&#xA0;&#xA0;&#xA0; }<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread2</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">; <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; getThread</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">.interrupt</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testMTCInterruptedAcquire</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Throwable </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">TestFramework.runOnce</span><span class="java8">( </span><span class="java4">new </span><span class="java10">MTCInterruptedAcquire</span><span class="java8">() )</span><span class="java10">;<br />
							</span><span class="java8">}&#xA0;&#xA0;&#xA0; </span></code></div>
								
						</td>
					</tr>				
					<tr class="row-b">
						<td>
							Plain Version
						</td>
					</tr>
					<tr class="row-a">
						<td>
				
							<div class="java"><code class="java"><span class="java4">volatile </span><span class="java9">boolean </span><span class="java10">threadFailed;<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">setUp</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">false</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">threadShouldThrow</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">true</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; fail</span><span class="java8">(</span><span class="java5">&#34;should throw exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">tearDown</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">assertFalse</span><span class="java8">(</span><span class="java10">threadFailed</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testInterruptedAcquire</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">Semaphore s = </span><span class="java4">new </span><span class="java10">Semaphore</span><span class="java8">(</span><span class="java7">0</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; Thread t = </span><span class="java4">new </span><span class="java10">Thread</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Runnable</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">run</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">s.acquire</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; threadShouldThrow</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch</span><span class="java8">(</span><span class="java10">InterruptedException success</span><span class="java8">){}<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; }<br />
							&#xA0;&#xA0;&#xA0; })</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; t.start</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">Thread.sleep</span><span class="java8">(</span><span class="java7">50</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; t.interrupt</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; t.join</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch</span><span class="java8">(</span><span class="java10">InterruptedException e</span><span class="java8">){<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">fail</span><span class="java8">(</span><span class="java5">&#34;Unexpected exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}</span></code></div>
				
						</td>
					</tr>
				</table>
				
				<p><strong>MultithreadedTC</strong> provides some convenience methods for getting access to a 
				thread. The call to <code>getThread(n)</code> returns a reference to the thread running the 
				method <code>threadn()</code> where <i>n</i> is an integer. The more general method, 
				<code>getThreadByName(&quot;threadMethod&quot;)</code> returns a reference to the thread running the 
				method <code>threadMethod().</code> So <code>getThread(1)</code> is equivalent to
				<code>getThreadByName(&quot;thread1&quot;)</code>. </p>
				
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				<h2>Example 3: Thread Ordering</h2>
				<p>Most of the time, <code>waitForTick()</code> is used to wait until another thread blocks, 
				as in Example 2. Occasionally though, it is useful for coordinating two or more 
				threads that do not contain any blocking. This example shows that even though, 
				the same can be accomplished using <code>CountDownLatch</code>s, the <strong>MultithreadedTC</strong> version 
				using <code>waitForTick()</code> is easier to write and understand.</p>

				<table>
					<tr class="row-b">
						<td>
							MultithreadedTC Version
						</td>
					</tr>
					<tr class="row-a">
						<td>

							<div class="java"><code class="java"><span class="java4">class </span><span class="java10">MTCThreadOrdering </span><span class="java4">extends </span><span class="java10">MultithreadedTestCase </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">AtomicInteger ai;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">initialize</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">ai = </span><span class="java4">new </span><span class="java10">AtomicInteger</span><span class="java8">(</span><span class="java7">0</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread1</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">0</span><span class="java10">, </span><span class="java7">1</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S1<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">3</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span class="java3">// S4<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread2</span><span class="java8">() {&#xA0;&#xA0; <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">1</span><span class="java10">, </span><span class="java7">2</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S2<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">3</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span><span class="java3">// S4<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread3</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">2</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">2</span><span class="java10">, </span><span class="java7">3</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S3<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testMTCThreadOrdering</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Throwable </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">TestFramework.runOnce</span><span class="java8">( </span><span class="java4">new </span><span class="java10">MTCThreadOrdering</span><span class="java8">() )</span><span class="java10">;<br />
							</span><span class="java8">}</span></code></div>
								
						</td>
					</tr>				
					<tr class="row-b">
						<td>
							CountDownLatch version
						</td>
					</tr>
					<tr class="row-a">
						<td>

							<div class="java"><code class="java"><span class="java4">volatile </span><span class="java9">boolean </span><span class="java10">threadFailed;<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">setUp</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">false</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">tearDown</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">assertFalse</span><span class="java8">(</span><span class="java10">threadFailed</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">unexpectedException</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">true</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; fail</span><span class="java8">(</span><span class="java5">&#34;Unexpected exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testLatchBasedThreadOrdering</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">InterruptedException </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">CountDownLatch c1 = </span><span class="java4">new </span><span class="java10">CountDownLatch</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">CountDownLatch c2 = </span><span class="java4">new </span><span class="java10">CountDownLatch</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">CountDownLatch c3 = </span><span class="java4">new </span><span class="java10">CountDownLatch</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">AtomicInteger ai = </span><span class="java4">new </span><span class="java10">AtomicInteger</span><span class="java8">(</span><span class="java7">0</span><span class="java8">)</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; Thread t1 = </span><span class="java4">new </span><span class="java10">Thread</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Runnable</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">run</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">0</span><span class="java10">, </span><span class="java7">1</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S1<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">c1.countDown</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; c3.await</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span><span class="java3">// S4<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch </span><span class="java8">(</span><span class="java10">Exception e</span><span class="java8">) {&#xA0; <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">unexpectedException</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; }<br />
							&#xA0;&#xA0;&#xA0; })</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; Thread t2 = </span><span class="java4">new </span><span class="java10">Thread</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Runnable</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">run</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">c1.await</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">1</span><span class="java10">, </span><span class="java7">2</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S2<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">c2.countDown</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; c3.await</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertEquals</span><span class="java8">(</span><span class="java10">ai.get</span><span class="java8">()</span><span class="java10">, </span><span class="java7">3</span><span class="java8">)</span><span class="java10">;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span><span class="java3">// S4<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch </span><span class="java8">(</span><span class="java10">Exception e</span><span class="java8">) {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">unexpectedException</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; }<br />
							&#xA0;&#xA0;&#xA0; })</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; t1.start</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; t2.start</span><span class="java8">()</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; c2.await</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; assertTrue</span><span class="java8">(</span><span class="java10">ai.compareAndSet</span><span class="java8">(</span><span class="java7">2</span><span class="java10">, </span><span class="java7">3</span><span class="java8">))</span><span class="java10">; </span><span class="java3">// S3&#xA0;&#xA0;&#xA0; <br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">c3.countDown</span><span class="java8">()</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; t1.join</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; t2.join</span><span class="java8">()</span><span class="java10">;<br />
							</span><span class="java8">}</span></code></div>
				
						</td>
					</tr>
				</table>
								
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				
				<h2>Example 4: Allowing Timeouts</h2>
				
				<p>Some concurrent abstractions rely on timing. An example is 
				the <code>offer()</code> method on the <code>ArrayBlockingQueue</code>, 
				which tries to add an element to the buffer and blocks for a 
				specified time limit if the buffer is full. If the <code>offer()</code> 
				times out, the method returns <code>false</code>. This example 
				shows how <code>waitForTick()</code> can interact with these 
				timing elements.</p>
				<p>If the goal of the test is to cause a timeout in one thread, 
				we may want to prevent the clock from advancing in another 
				thread. MultithreadedTC provides the methods <code>freezeClock()</code> 
				and <code>unfreezeClock()</code>, to respectively disable and 
				re-enable the clock thread. (The clock thread can still kill a 
				test that is running for too long when it is disabled.) </p>
				<p>In this test, the first <code>offer()</code> is allowed to timeout, the second 
				<code>offer()</code> is interrupted. To guarantee that the 
				interrupt does not occur on the first <code>offer()</code>, it uses
				<code>freezeClock()</code>
				   to freeze the clock during the first offer. Thread 2 is 
				awakened when Thread 1 blocks on the second <code>offer()</code>. 
				(Note that the timeout in the <code>offer()</code> statement 
				should be much longer than the clock period (<a href="docs/edu/umd/cs/mtc/TestFramework.html">see 
				Javadoc</a>) to ensure that the clock thread has a chance to 
				wake up.) Obviously tests like this that inherently include 
				timing do not play well in a debugger.</p>
							
				<table>
					<tr class="row-b">
						<td>
							MultithreadedTC Version
						</td>
					</tr>
					<tr class="row-a">
						<td>

							<div class="java"><code class="java"><span class="java4">class </span><span class="java10">MTCTimedOffer </span><span class="java4">extends </span><span class="java10">MultithreadedTestCase </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">ArrayBlockingQueue&lt;Object&gt; q;<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java16">@Override </span><span class="java4">public </span><span class="java9">void </span><span class="java10">initialize</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">q = </span><span class="java4">new </span><span class="java10">ArrayBlockingQueue&lt;Object&gt;</span><span class="java8">(</span><span class="java7">2</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread1</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">q.put</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; q.put</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">())</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; freezeClock</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertFalse</span><span class="java8">(</span><span class="java10">q.offer</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">()</span><span class="java10">, <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java7">25</span><span class="java10">, TimeUnit.MILLISECONDS</span><span class="java8">))</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; unfreezeClock</span><span class="java8">()</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; q.offer</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">()</span><span class="java10">, <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java7">2500</span><span class="java10">, TimeUnit.MILLISECONDS</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; fail</span><span class="java8">(</span><span class="java5">&#34;should throw exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch </span><span class="java8">(</span><span class="java10">InterruptedException success</span><span class="java8">){ <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">assertTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">; <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							&#xA0;&#xA0;&#xA0; }<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">thread2</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">waitForTick</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; getThread</span><span class="java8">(</span><span class="java7">1</span><span class="java8">)</span><span class="java10">.interrupt</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testMTCTimedOffer</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Throwable </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">TestFramework.runOnce</span><span class="java8">( </span><span class="java4">new </span><span class="java10">MTCTimedOffer</span><span class="java8">() )</span><span class="java10">;<br />
							</span><span class="java8">}</span></code></div>
								
						</td>
					</tr>				
					<tr class="row-b">
						<td>
							Plain Version
						</td>
					</tr>
					<tr class="row-a">
						<td>

							<div class="java"><code class="java"><span class="java4">volatile </span><span class="java9">boolean </span><span class="java10">threadFailed;<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">setUp</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">false</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">threadShouldThrow</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">true</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; fail</span><span class="java8">(</span><span class="java5">&#34;should throw exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">threadAssertFalse</span><span class="java8">(</span><span class="java9">boolean </span><span class="java10">b</span><span class="java8">) {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">if </span><span class="java8">(</span><span class="java10">b</span><span class="java8">) {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">threadFailed = </span><span class="java4">true</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; assertFalse</span><span class="java8">(</span><span class="java10">b</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}<br />
							<br />
							</span><span class="java4">protected </span><span class="java9">void </span><span class="java10">tearDown</span><span class="java8">() </span><span class="java4">throws </span><span class="java10">Exception </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java10">assertFalse</span><span class="java8">(</span><span class="java10">threadFailed</span><span class="java8">)</span><span class="java10">;<br />
							</span><span class="java8">}<br />
							<br />
							</span><span class="java4">public </span><span class="java9">void </span><span class="java10">testTimedOffer</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">final </span><span class="java10">ArrayBlockingQueue&lt;Object&gt; q = <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">new </span><span class="java10">ArrayBlockingQueue&lt;Object&gt;</span><span class="java8">(</span><span class="java7">2</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; Thread t = </span><span class="java4">new </span><span class="java10">Thread</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Runnable</span><span class="java8">() {&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">public </span><span class="java9">void </span><span class="java10">run</span><span class="java8">() {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">q.put</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; q.put</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">())</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; threadAssertFalse</span><span class="java8">(</span><span class="java10">q.offer</span><span class="java8">( </span><span class="java4">new </span><span class="java10">Object</span><span class="java8">()</span><span class="java10">,<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java7">25</span><span class="java10">, TimeUnit.MILLISECONDS</span><span class="java8">))</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; q.offer</span><span class="java8">(</span><span class="java4">new </span><span class="java10">Object</span><span class="java8">()</span><span class="java10">, <br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java7">2500</span><span class="java10">, TimeUnit.MILLISECONDS</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; threadShouldThrow</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch </span><span class="java8">(</span><span class="java10">InterruptedException success</span><span class="java8">){}<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; }<br />
							&#xA0;&#xA0;&#xA0; })</span><span class="java10">;<br />
							<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java4">try </span><span class="java8">{<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">t.start</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; Thread.sleep</span><span class="java8">(</span><span class="java7">50</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; t.interrupt</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; t.join</span><span class="java8">()</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">} </span><span class="java4">catch </span><span class="java8">(</span><span class="java10">Exception e</span><span class="java8">) {<br />
							&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; </span><span class="java10">fail</span><span class="java8">(</span><span class="java5">&#34;Unexpected exception&#34;</span><span class="java8">)</span><span class="java10">;<br />
							&#xA0;&#xA0;&#xA0; </span><span class="java8">}<br />
							}</span></code></div>
				
						</td>
					</tr>
				</table>

				
				<p class="post-footer align-right">
					<a href="#pagetop" class="readmore">Back to Top</a>
				</p>
				
				<p>&nbsp;</p>
				
			</div>

		<!-- content-wrap ends here -->
		</div>

		<!--footer starts here-->
		<div id="footer">

			<p>
			&copy; 2007 <strong>University of Maryland</strong> |
			Design by: <a href="http://www.styleshout.com/">styleshout</a> |
			Valid <a href="http://validator.w3.org/check?uri=referer">XHTML</a> |
			<a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>

   		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

			<!-- <a href="index.html">Home</a>&nbsp;|&nbsp;
			<a href="index.html">Sitemap</a>&nbsp;|&nbsp;
			<a href="index.html">RSS Feed</a> -->
   		</p>

		</div>

<!-- wrap ends here -->
</div>

</body>
</html>
